
package com.twb.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.twb.core.base.Result;
import com.twb.core.base.controller.BaseController;
import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.security.annotation.Auth;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.Results;
import com.twb.system.entity.twbTenantConfig.TwbTenantConfig;
import com.twb.system.entity.twbTenantConfig.dto.GetTenantConfigByIdDto;
import com.twb.system.entity.twbTenantConfig.dto.SearchTenantConfigDto;
import com.twb.system.entity.twbTenantConfig.vo.SaveTenantConfigVo;
import com.twb.system.entity.twbTenantConfig.vo.SearchTenantConfigVo;
import com.twb.system.entity.twbTenantConfig.vo.UpdateTenantConfigByIdVo;
import com.twb.system.service.ITwbTenantConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author DSY
 * @ClassName: TenantConfigController
 * @Description: 租户配置管理Controller
 * @date 2022-12-30 16:00:28
 */
@RestController
@RequestMapping("/system/tenantConfig")
@Api(value = "TwbTenantConfigController", tags = "租户配置管理")
public class TwbTenantConfigController extends BaseController {

    @Autowired
    ITwbTenantConfigService tenantConfigService;

    /**
     * @param vo
     * @return
     * @Title: search
     * @Description: 分页查询
     * @author DSY
     * @date 2022-12-30 16:00:28
     */
    @GetMapping("/search")
    @ApiOperation(value = "分页查询")
    public Result<TwbPageInfo<SearchTenantConfigDto>> search(SearchTenantConfigVo vo) {
        TwbPageInfo<SearchTenantConfigDto> result = tenantConfigService.searchTenantConfig(vo);
        return Results.successWithData(result);
    }

    /**
     * @param vo
     * @return
     * @Title: searchSum
     * @Description: 列表查询合计
     * @author DSY
     * @date 2022-12-30 16:00:28
     */
    @Auth(verifyURL = false)
    @GetMapping("/searchSum")
    @ApiOperation(value = "列表查询合计")
    public Result<SearchTenantConfigDto> searchSum(SearchTenantConfigVo vo) {
        SearchTenantConfigDto result = tenantConfigService.searchTenantConfigSum(vo);
        return Results.successWithData(result);
    }

    /**
     * @param id
     * @return
     * @Title: getById
     * @Description: 根据ID获取详情
     * @author DSY
     * @date 2022-12-30 16:00:28
     */
    @GetMapping("/getById")
    @ApiOperation(value = "根据ID获取详情")
    public Result<GetTenantConfigByIdDto> getById(
        @NotBlank
        @RequestParam
        @ApiParam(value = "主键", name = "id", required = true)
            String id
    ) {
        TwbTenantConfig tenantConfig = tenantConfigService.getById(id);
        if (tenantConfig == null) {
            return Results.failure("数据不存在");
        }
        GetTenantConfigByIdDto dto = new GetTenantConfigByIdDto();
        BeanUtils.copyNotNullProperties(tenantConfig, dto);
        return Results.successWithData(dto);
    }

    /**
     * 根据类型获取详情
     *
     * @param configType 配置类型
     * @return {@link Result }<{@link GetTenantConfigByIdDto }>
     * @author DHL
     * @date 2023-01-05 02:41:38
     */
    @GetMapping("/getByType")
    @ApiOperation(value = "根据类型获取详情")
    public Result<GetTenantConfigByIdDto> getByType(
        @NotBlank
        @RequestParam
        @ApiParam(value = "类型", name = "configType", required = true)
            String configType
    ) {
        QueryWrapper<TwbTenantConfig> configQueryWrapper = new QueryWrapper<>();
        configQueryWrapper.eq("config_type", configType);
        configQueryWrapper.eq("tenant_id", getLoginUser().getTenantId());
        TwbTenantConfig tenantConfig = tenantConfigService.getOne(configQueryWrapper);
        if (tenantConfig == null) {
            return Results.failure("数据不存在");
        }
        GetTenantConfigByIdDto dto = new GetTenantConfigByIdDto();
        BeanUtils.copyNotNullProperties(tenantConfig, dto);
        return Results.successWithData(dto);
    }

    /**
     * @param vo
     * @return
     * @Title: save
     * @Description: 保存信息
     * @author DSY
     * @date 2022-12-30 16:00:28
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存信息")
    public Result<String> save(@RequestBody @Valid SaveTenantConfigVo vo) {
        boolean result = tenantConfigService.saveTenantConfig(vo);
        if (result) {
            return Results.success("保存成功");
        } else {
            return Results.failure("保存失败");
        }
    }

    /**
     * @param vo
     * @return
     * @Title: updateById
     * @Description:更新信息
     * @author DSY
     * @date 2022-12-30 16:00:28
     */
    @PutMapping("/updateById")
    @ApiOperation(value = "更新信息")
    public Result<String> updateById(@RequestBody @Valid UpdateTenantConfigByIdVo vo) {
        boolean result = tenantConfigService.updateTenantConfigById(vo);
        if (result) {
            return Results.success("更新成功");
        } else {
            return Results.failure("更新失败");
        }
    }

    /**
     * @param vo
     * @param response
     * @throws Exception
     * @Title exportExcel
     * @Description 导出Excel
     * @author DSY
     * @date 2022-12-30 16:00:28
     */
    @PostMapping("/exportExcel")
    @ApiOperation(value = "导出excel")
    public void exportExcel(@RequestBody SearchTenantConfigVo vo, HttpServletResponse response) throws Exception {

        tenantConfigService.exportExcel(vo, response);
    }

    /**
     * @Title: deleteBatchByIds
     * @Description:批量删除信息
     * @param ids
     * @return
     * @author DSY
     * @date 2022-12-30 16:00:28
     */
    // @Auth(verifyToken = true)
    // @PostMapping("/batchDeleteByIds")
    // @ApiOperation(value = "批量删除信息")
    // public Result<String> deleteBatchByIds(
    //   @NotBlank
    //   @RequestParam
    //   @ApiParam(value = "主键集合", name = "ids", required = true
    // ) String ids) {
    //   boolean result = tenantConfigService.deleteTenantConfigByIds(ids);
    //   if (result) {
    //     return Results.success("删除成功");
    //   } else {
    //     return Results.failure("删除失败");
    //   }
    // }

    /**
     * @param ids
     * @return
     * @Title: logicBatchDeleteByIds
     * @Description: 批量逻辑删除信息
     * @author DSY
     * @date 2022-12-30 16:00:28
     */
    @Auth(verifyToken = true)
    @PostMapping("/logicBatchDeleteByIds")
    @ApiOperation(value = "批量逻辑删除信息")
    public Result<String> logicBatchDeleteByIds(
        @NotBlank
        @RequestParam
        @ApiParam(value = "主键集合", name = "ids", required = true
        ) String ids) {
        int result = tenantConfigService.logicBatchDeleteTenantConfigByIds(ids);
        if (result > 0) {
            return Results.success("删除成功");
        } else {
            return Results.failure("删除失败");
        }
    }


    /**
     * 获取企业微信应用ID
     *
     * @param domain     域
     * @return {@link Result }<{@link String }>
     * @author DHL
     * @date 2023-01-13 02:37:14
     */
    @Auth(verifyToken = false, verifyURL = false)
    @GetMapping("/getWxCpAgentid")
    public Result<String> getWxCpAgentid(@RequestParam(name = "domain", required = true) String domain) {
        String agentid = tenantConfigService.getWxCpAgentid(domain);
        return Results.successWithData(agentid);
    }
}
